const { resolve } = require('path')

const { jgcl } = require(resolve(__dirname, '../../utils/utils'))

const Articles = require(resolve(__dirname, '../../database/dbModule/article'))

// 添加文章
const addArticle = async (ctx) => {
  try {
    const data = ctx.request.body
    const {
      title,
      introduction,
      articleCover,
      category,
      tag,
      commentLikeFavorites,
      articleStatus,
      articleContent,
      createTime = new Date().getTime(),
      updateTime = 0,
      isDel = false
    } = data
    const findRes = await Articles.findOne({ title, isDel: false })
    if (findRes === null) {
      const res = await Articles.create({
        title,
        introduction,
        articleCover,
        category,
        tag,
        commentLikeFavorites,
        articleStatus,
        articleContent,
        createTime,
        updateTime,
        isDel
      })
      if (res === null) {
        jgcl(ctx, 2011, '文章添加失败!')
      } else {
        jgcl(ctx, 200, '文章添加成功!', res)
      }
    } else {
      jgcl(ctx, 2010, '文章已存在', title)
    }
  } catch (err) {
    jgcl(ctx, 2011, '文章添加失败!', err)
  }
}

// 查询所有文章
const getArticle = async (ctx) => {
  try {
    const { id } = ctx.query
    if (id) {
      const findRes = await Articles.findOne({ _id: id, isDel: false })
      if (findRes !== null) {
        jgcl(ctx, 200, '查询成功!', findRes)
      } else {
        jgcl(ctx, 2015, '文章数据为空!')
      }
    } else {
      const num = await Articles.find({ isDel: false }).count()
      const findRes = await Articles.find({ isDel: false })
      if (findRes.length > 0) {
        jgcl(ctx, 200, '查询成功!', { total: num, list: findRes })
      } else {
        jgcl(ctx, 2015, '文章数据为空!')
      }
    }
  } catch (err) {
    jgcl(ctx, 2014, '查询失败!', err)
  }
}

// 获取文章总数
const getArticleTotal = async (ctx) => {
  try {
    const num = await Articles.find({ isDel: false }).count()

    jgcl(ctx, 200, '查询成功!', { total: num })
  } catch (err) {
    jgcl(ctx, 2014, '查询失败!')

  }
}

// 搜索文章
const findArticle = async (ctx) => {
  try {
    const { keyWord } = ctx.request.body

    const findRes = await Articles.find({
      isDel: false,
      title: { $regex: new RegExp(keyWord) }
    })
    if (findRes.length > 0) {
      jgcl(ctx, 200, '搜索成功!', findRes)
    } else {
      jgcl(ctx, 2015, '搜索数据为空!')
    }
  } catch (err) {
    jgcl(ctx, 2014, '搜索失败!', err)
  }
}

// 搜索文章
const getArticleOne = async (ctx) => {
  try {
    const { id } = ctx.request.body

    const findRes = await Articles.findOne({
      isDel: false,
      _id: id
    })
    if (findRes !== null) {
      jgcl(ctx, 200, '搜索成功!', findRes)
    } else {
      jgcl(ctx, 2015, '搜索数据为空!')
    }
  } catch (err) {
    jgcl(ctx, 2014, '搜索失败!', err)
  }
}

// 删除文章
const delArticle = async (ctx) => {
  try {
    const { id } = ctx.request.body
    const findRes = await Articles.findOne({ isDel: false, _id: id })
    if (findRes !== null) {
      const delRes = await Articles.updateOne({ _id: id }, { isDel: true })
      jgcl(ctx, 200, '删除成功!', delRes)
    } else {
      jgcl(ctx, 2012, '文章不存在!')
    }
  } catch (err) {
    jgcl(ctx, 2016, '删除失败!', err)
  }
}

// 修改文章
const upArticle = async (ctx) => {
  try {
    const data = ctx.request.body
    const {
      id,
      title,
      introduction,
      articleCover,
      category,
      tag,
      commentLikeFavorites,
      articleStatus,
      articleContent,
      updateTime = new Date().getTime(),
    } = data
    const findRes = await Articles.findOne({ _id: id, isDel: false })
    if (findRes === null) {
      jgcl(ctx, 2012, '文章不存在', title)
    } else {
      const res = await Articles.updateOne({ _id: id }, {
        title,
        introduction,
        articleCover,
        category,
        tag,
        commentLikeFavorites,
        articleStatus,
        articleContent,
        updateTime,
      })
      jgcl(ctx, 200, '文章修改成功!')
    }
  } catch (err) {
    jgcl(ctx, 2013, '文章修改失败!', err)
  }
}

// 修改为发布文章
const releaseArticle = async (ctx) => {
  try {
    const { id } = ctx.request.body

    const findRes = await Articles.findOne({ isDel: false, _id: id })

    if (findRes === null) {
      jgcl(ctx, 2012, '文章不存在')
    } else {
      const upRes = await Articles.updateOne({ _id: id }, { articleStatus: '已发布' })

      jgcl(ctx, 200, '文章发布成功!')
    }

  } catch (err) {
    jgcl(ctx, 2013, '文章发布失败!', err)
    console.log(err)
  }
}

// 查看数量
const lookArticle = async (ctx) => {
  try {
    const { id } = ctx.request.body
    const findRes = await Articles.findOne({ _id: id })
    const upRes = await Articles.updateOne(
      { _id: id, isDel: false },
      { commentLikeFavorites: `${parseInt(findRes.commentLikeFavorites.split('/')[0]) + 1}/${parseInt(findRes.commentLikeFavorites.split('/')[1])}` }
    )
    jgcl(ctx, 200, '成功！')

  } catch (err) {
    jgcl(ctx, 2013, '失败！')
    console.log(err)
  }

}
// 点赞数量
const discussArticle = async (ctx) => {
  try {
    const { id, num } = ctx.request.body
    const findRes = await Articles.findOne({ _id: id })
    const upRes = await Articles.updateOne(
      { _id: id, isDel: false },
      { commentLikeFavorites: `${parseInt(findRes.commentLikeFavorites.split('/')[0])}/${num}` }
    )
    jgcl(ctx, 200, '成功！')

  } catch (err) {
    jgcl(ctx, 2013, '失败！')
  }

}

const findArticleOne = async (ctx) => {
  try {
    const {
      type,
      title
    } = ctx.query

    if (type === 'classification') {
      const findRes = await Articles.find({ isDel: false, category: title })
      jgcl(ctx, 200, '查询成功!', findRes)
    } else if (type === 'tag') {
      const tagFindRes = await Articles.find({ isDel: false, tag: title })
      jgcl(ctx, 200, '查询成功!', tagFindRes)
    }
  } catch (err) {
    jgcl(ctx, 2014, '查询失败!', err)

  }
}

module.exports = {
  addArticle,
  getArticle,
  findArticle,
  delArticle,
  upArticle,
  releaseArticle,
  getArticleTotal,
  getArticleOne,
  lookArticle,
  discussArticle,
  findArticleOne
}